Skip to main content
ICT
Lesson A13 - Exceptions and File I/O
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

B. Handling Exceptions page 4 of 12

  1. There are three ways of handling a possible exception occurrence (we say that the exception is thrown). In some cases, such as when there is a possibility that a divide by zero exception might occur, the programmer has the option of not dealing with the exception at all. This can be useful in situations where the programmer has already taken steps to ensure that a denominator never becomes zero. The programmer may also attempt to fix the problem or skip over the problem. To do either of these, the programmer needs to catch any exception that may be thrown. To catch an exception, one must anticipate where the exception might occur and enclose that code in a try block. The try block is followed by a catch block that catches the exception (if it occurs) and performs the desired action.

  2. The general form of a try-catch statement is:

    try{
    try-block
    } catch (exception-type identifier){
    catch-block
    }

    1. The try-block refers to a statement or series of statements that might throw an exception. If no exception is thrown, all of the statements within the try-block will be executed. Once an exception is thrown, however, all of the statements following the exception in the try-block will be skipped.

    2. The catch-block refers to a statement or series of statements to be executed if the exception is thrown. A try block can be followed by more than one catch block. When an exception is thrown inside a try block, the first matching catch block will handle the exception.

    3. exception-type specifies what kind of exception object the catch block should handle. This can be specific or it can be general, i.e. IOException or just Exception. Exception by itself will catch any type of exception that comes its way.

    4. identifier is an arbitrary variable name used to refer to the exception-type object. Any operations done on the exception object or any methods called will use this identifier

  3. The try and catch blocks work in a very similar manner to the if-else statement and can be placed anywhere that normal code can be placed.

  1. If an exception is thrown anywhere in the try block which matches one of the exception-types named in a catch block, then the code in the appropriate catch block is executed. If the try block executes normally, without an exception, the catch block is ignored.
  1. Here is an example of try and catch:

    int quotient;
    int numerator = 23;
    int denominator = 0;
    try{
      quotient = numerator/denominator;
      System.out.println("The answer is: " + quotient);
    } catch (ArithmeticException e){
      System.out.println("Error: Division by zero");
    }

The value of denominator is zero so an ArithmeticException will be thrown whenever numerator is divided by denominator. The catch block will catch the exception and print an error message. The println() statement in the try block will not be executed because the exception occurs before the program reaches that line of code. Once an exception is encountered, the rest of the lines of code in the try-block will be ignored. If the value of denominator is not zero, the code in the catch block will be ignored and the println() statement will output the result of the division. Either way, the program continues executing at the next statement after the catch block.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.